﻿@using Website.CRUDServices
@using Location = Website.Models.Domain.Location
@using Profile = Website.Models.Domain.Profile
@using Region = Website.Models.Domain.Region
@Model
@{
    this.ViewBag.Title = "Network";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Presentations = this.ViewData["Presentations"] as List<Presentation>;
    var Regions = this.ViewData["Regions"] as List<Region>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var ProfileView = this.ViewData["Profiles"] as Website.Models.ViewModels.ProfilesView;
    var NActivity = this.Model as Activity;
    Guid otherId = Guid.Empty;

    // errorhandling
    var errorMsg = new HtmlString(string.Empty);
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

}
<script type="text/javascript">
    //toggle script
    function toggle(id) {
        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }
</script>
<!--toggle topic elaboration -->
@foreach (Presentation pres in Presentations.Where(x => (NActivity.NA.Presentations ?? new Guid[0]).Contains(x.ID)))
{
    if (pres.Name == "Andet")
    {
    <script>
        $(function () {
            toggle('andet_holder');
        });
    </script>
    }

}
<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>

@using (this.Html.BeginForm())
{
    <div class="error">
        @errorMsg
    </div>
    // hidden field to hold ID of the object
    <input type="hidden" id="ID" name="ID"  readonly="readonly" value="@NActivity.ID" />
    // hidden field to hold ID of the objects object
    <input type="hidden" id="ID2" name="ID2"  readonly="readonly" value="@NActivity.NA.ID" />
    // date
    <div class="formBullet">Dato</div>
    <input type="text" id="datepicker" name="datepicker" readonly="readonly" value="@NActivity.TimeOfActivity" />

    // location
    <div class="formBullet">Lokation</div>
    <div class="formEntry">
        <select name="Location" id="Location">
            <option value="@Guid.Empty">Vælg en lokation</option>
            @foreach (Location loc in ProfileView.Locations.OrderBy(x => x.Name))
            {
                if (loc.ID == NActivity.Location)
                {
                <option value="@loc.ID" selected="selected">@loc.Name</option>
                }
                else
                {
                <option value="@loc.ID">@loc.Name</option>
                }
            }
        </select>
        <input type="button" value="Ny" id="newLocation" />
    </div>
    
            // Type chooser
    <div class="formBullet">Hvilket type netværk er der tale om?</div>
    <div>
        @if (NActivity.NA.IsNetworkingEvent == true)
        {
            <input type="radio" name="NetworkType" value="IsNetworkingEvent" checked>
        }
        else
        {
            <input type="radio" name="NetworkType" value="IsNetworkingEvent">
        }Netværksmøde<br />
        @if (NActivity.NA.IsConferenceEvent == true)
        {
            <input type="radio" name="NetworkType" value="IsConferenceEvent" checked>
        }
        else
        {
            <input type="radio" name="NetworkType" value="IsConferenceEvent">
        }Konference<br />
        @if (NActivity.NA.IsDelegationTravel == true)
        {
            <input type="radio" name="NetworkType" value="IsDelegationTravel" checked>
        }
        else
        {
            <input type="radio" name="NetworkType" value="IsDelegationTravel">
        }
        Delegationsrejse<br />
        @if (NActivity.NA.IsProductLaunch == true)
        {
            <input type="radio" name="NetworkType" value="IsProductLaunch" checked>
        }
        else
        {
            <input type="radio" name="NetworkType" value="IsProductLaunch">
        }Produkt lancering<br />
    </div>

        // ownerProjects chooser
    <div class="formBullet">Alle projekter tilknyttet</div>
    <div class="formEntry">
        <select multiple="multiple" name="OwnerProjects" id="OwnerProjects">
            @foreach (Project proj in Projects.Where(x => (NActivity.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID" selected="selected">@proj.Name</option>
            }
            @foreach (Project proj in Projects.Where(x => !(NActivity.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID">@proj.Name</option>
            }
        </select>
    </div>

            // profiles chooser
    <div class="formBullet">Deltagere</div>
    <div class="formEntry">
        <select multiple="multiple" name="Attendees" id="Attendees">
            @foreach (Profile prof in ProfileView.Profiles.Where(x => (NActivity.Attendees ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID" selected="selected">@prof.Name</option>
            }
            @foreach (Profile prof in ProfileView.Profiles.Where(x => !(NActivity.Attendees ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
    </div>

            // Product type
    <div class="formBullet">Produkt type</div>
    <input type="text" id="ProductType" name="ProductType" value="@NActivity.NA.ProductType"/>

            // attendance
    <div class="formBullet">Deltagelse</div>
    <input type="text" id="Attendance" name="Attendance" value="@NActivity.NA.Attendance"/>

            // booth
    <div class="formBullet">Stand</div>
    <input type="text" id="Booth" name="Booth" value="@NActivity.NA.Booth"/>

            // Organizer chooser
    <div class="formBullet">Arrangør</div>
    <div class="formEntry">
        <select name="Organizer" id="Organizer">
            @foreach (Profile prof in ProfileView.Profiles)
            {
                if (prof.ID == NActivity.NA.Organizer)
                {
                <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                else
                {
                <option value="@prof.ID">@prof.Name</option>
                }
            }
        </select>
    </div>

            // Profiles chooser
    <div class="formBullet">Understøttelse af virksomhedsdeltagelse</div>
    <div class="formEntry">
        <select multiple="multiple" name="SupportForBusinessAttendance" id="SupportForBusinessAttendance">
            @foreach (Profile prof in ProfileView.Profiles.Where(x => (NActivity.NA.SupportForBusinessAttendance ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID" selected="selected">@prof.Name</option>
            }
            @foreach (Profile prof in ProfileView.Profiles.Where(x => !(NActivity.NA.SupportForBusinessAttendance ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
    </div>

            // Presentations chooser
    <div class="formBullet">Præsentationer</div>
    <div class="formEntry">
        <select multiple="multiple" name="Presentations" id="Presentations">
            @foreach (Presentation pres in Presentations.Where(x => (NActivity.NA.Presentations ?? new Guid[0]).Contains(x.ID)))
            {
                if (pres.Name == "Andet")
                {
                    otherId = pres.ID;
                }
                <option value="@pres.ID" selected="selected">@pres.Name</option>
            }
            @foreach (Presentation pres in Presentations.Where(x => !(NActivity.NA.Presentations ?? new Guid[0]).Contains(x.ID)))
            {
                if (pres.Name == "Andet")
                {
                    otherId = pres.ID;
                }

                <option value="@pres.ID">@pres.Name</option>
            }
        </select>
    </div>

            // field for specifying "andet"
    <div class="formEntry open_hidden" id="andet_holder">
        <div class="formBullet">Specificér "Andet"</div>
        <textarea name="Andet">@NActivity.NA.PresentationComment</textarea>
    </div>

    <div class="formBullet">Kommentarer</div>
    <div class="formEntry">
        <textarea name="Commentary">@NActivity.Commentary</textarea>
    </div>

    <div class="buttonHolder">
        <input type="submit" value="Gem" />
    </div>
}

<script>

    @*
        Og nu da det er rykket herned kan du bruge @otherID til at lave dit script.
        Alt hvad der bliver skrevet med et @ bliver parset (server-side) før siden er endeligt loadet.
    *@
    $(function () {
        $('#SupportForBusinessAttendance').multiSelect();
        $('#Attendees').multiSelect();
        $('#OwnerProjects').multiSelect();


        //choosing "andet" will pop up an additional text-box for input on the subject

        $('#Presentations').multiSelect({
            afterSelect: function (value) {
                if (value == "@otherId.ToString()") {
                    toggle("andet_holder");
                }
            },

            afterDeselect: function (value) {
                if (value == "@otherId.ToString()") {
                    toggle("andet_holder");
                }
            }
        });
        //handle locations
        $("#newLocation").click(function () {
            $("#dialog").dialog({
                width: "340px",
                modal: true
            });
        });
        $('#CreateLocationForm').submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        // We have a JSON object in case of success
                        $('#Locations')
                            .append($("<option></option>")
                                .attr("value", result.locationID)
                                .text(result.locationName))
                            .val(result.locationID);


                        $("#dialog").dialog('close');
                    } else {
                        // We have the partial with errors in case of failure
                        // so all we have to do is update the DOM
                        $('#CreateLocationForm').html(result);
                    }
                }
            });
        });

    });
</script>

<div id="dialog" title="Opret ny lokation" style="display: none;">
    @Html.Partial("~/Views/Locations/_CreateLocationPartial.cshtml", new Location())
</div>
